#                                                                    -*-perl-*-

$description = "The following test creates a makefile to test the -I option.";

$details = "\
This test tests the -I option by including a filename in
another directory and giving make that directory name
under -I in the command line.  Without this option, the make
would fail to find the included file.  It also checks to make
sure that the -I option gets passed to recursive makes.";

use File::Spec;

# Create a directory and put a makefile in it.
# We can't put it in the current directory since that's automatically searched
# anyway.
my $subdir = 'idir';
mkdir($subdir, 0777);

my $included = 'ifile.mk';
my $ipath = File::Spec->catfile($subdir, $included);
create_file($ipath, "
ANOTHER:
\t\@echo This is another included makefile
recurse:
\t\@\$(MAKE) ANOTHER -f \$(main_makefile)\n");

my $nosuch = "#MAKEFILE#:5: $included: $ERR_no_such_file
#MAKE#: *** No rule to make target '$included'.  Stop.\n";


# Verify that we get an error if we don't have -I
run_make_test(qq!
main_makefile := \$(firstword \$(MAKEFILE_LIST))
all:
\t\@echo There should be no errors for this makefile
include $included
!,
              '', $nosuch, 512);

# Check basic -I works
run_make_test(undef, "-I $subdir all",
              "There should be no errors for this makefile\n");

# Check that the included target works
run_make_test(undef, "-I $subdir ANOTHER",
              "This is another included makefile\n");

# Check that -I is passed down through MAKEFLAGS
run_make_test(undef, "-I $subdir recurse",
              "#MAKE#[1]: Entering directory '#PWD#'
This is another included makefile
#MAKE#[1]: Leaving directory '#PWD#'\n");

# Verify that we get an error if we add -I- to delete previous includes
run_make_test(undef, "-I $subdir -I- all", $nosuch, 512);

# Make another directory with the same name and make sure the right one is
# chosen if -I- stops the path.

mkdir('idir2', 0777);
my $ipath2 = File::Spec->catfile('idir2', $included);
create_file($ipath2, "This is a bad makefile!!\n");

run_make_test(undef, "-I idir2 -I $subdir ANOTHER",
              "$included:1: *** missing separator.  Stop.\n", 512);

run_make_test(undef, "-I idir2 -I - -I $subdir ANOTHER",
              "This is another included makefile\n");

# Check that -I- is passed down through MAKEFLAGS
run_make_test(undef, "-I idir2 -I - -I $subdir recurse",
              "#MAKE#[1]: Entering directory '#PWD#'
This is another included makefile
#MAKE#[1]: Leaving directory '#PWD#'\n");

unlink($ipath2);
rmdir('idir2');

# The only way to check if -I- voids included directories is to see if a file
# exists in one and try to include it.  We very likely can't add our own files
# to the default directories since they're probably write-protected.  This
# won't work if none of the default directories contain any files :-/

create_file('defaultdirs.mk', "\$(info \$(.INCLUDE_DIRS))\nall:;\@:\n");
my $cmd = subst_make_string("#MAKEPATH# -f defaultdirs.mk");
my @dirs = `$cmd`;
my $dirs = $dirs[0];
$dirs =~ s/\r?\n//g;
unlink('defaultdirs.mk');

my $fn = undef;
foreach my $dn (split ' ', $dirs) {
    # On Windows the default is "." which is bogus!
    if ($dn ne '.') {
        my @files = glob(File::Spec->catfile($dn, "*"));
        if (@files) {
            (undef, undef, $fn) = File::Spec->splitpath($files[0]);
            last;
        }
    }
}

if ($fn) {
    run_make_test("
all:;
include $fn
",
                  '-I-', "#MAKEFILE#:3: $fn: $ERR_no_such_file
#MAKE#: *** No rule to make target '$fn'.  Stop.\n", 512);
}

unlink($ipath);
rmdir($subdir);

1;
